package com.gitbl.webchat.db;

import com.alibaba.fastjson.JSONObject;
import com.gitbl.webchat.message.RoomMessage;
import com.gitbl.webchat.message.RoomsNodeMessage;
import com.gitbl.webchat.service.RoomService;
import com.gitbl.webchat.utils.RedisHelper;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//redis 操作类
public class DbHelper {
    ShardedJedisPool redispool;
    public DbHelper(String[] hosts){
        redispool = RedisHelper.newPool(hosts);
    }
    public RoomMessage getRoom(String id) {
        ShardedJedis rd = redispool.getResource();
        try{
            String roomstr = rd.get(RoomService.KEY_ROOM_PRE+id);
            return JSONObject.parseObject(roomstr,RoomMessage.class);
        }finally {
            rd.close();
        }
    }
    public List<RoomsNodeMessage> getAllNodes(){
        ShardedJedis rd = redispool.getResource();
        try {
            List<byte[]> keys = (List<byte[]>) rd.sendCommand(Protocol.Command.KEYS,  RoomService.KEY_NODE_PRE + "*");
            byte[][] keysarg = keys.toArray(new byte[keys.size()][]);
            List<RoomsNodeMessage> res = new ArrayList<>(keys.size());
            List<byte[]> nodessrc = (List<byte[]>) rd.sendCommand(Protocol.Command.MGET, keysarg );
            for (byte[] nodesrc: nodessrc
                 ) {
                res.add(JSONObject.parseObject(nodesrc,RoomsNodeMessage.class));
            }
            return res;
        }finally {
            rd.close();
        }
    }
    //模糊查询部分房间
    public List<RoomMessage> queryLessRoom(String title){
        ShardedJedis rd = redispool.getResource();
        try {
            String cursor = ScanParams.SCAN_POINTER_START;

            List<String> roomkeys = new LinkedList<>();
            List<RoomMessage> rooms = new ArrayList<>();
            while (true) {
                List<Object> sscanResult = (List<Object>) rd.sendCommand(Protocol.Command.SCAN, cursor, "MATCH", RoomService.KEY_ROOM_TITLE_PRE  + "*"+title+"*.*");
                //cursor = new String((byte[]) sscanResult.get(0));
                List<byte[]> scanResult = (List<byte[]>) sscanResult.get(1);

                for (int m = 0; m < scanResult.size(); m++) {
                    String mapentry = new String(scanResult.get(m));
                    String[] ksplarr = mapentry.split("\\.");

                    roomkeys.add(RoomService.KEY_ROOM_PRE +ksplarr[ksplarr.length-1]);
                }
                break;
                //不需要查完所有数据
//                if ("0".equals(cursor)) {
//                    break;
//                }
            }
            for (String k : roomkeys
            ) {
                String nodestr = rd.get(k);
                RoomMessage node = JSONObject.parseObject(nodestr, RoomMessage.class);
                if (node != null) {
                    rooms.add(node);
                }
            }
            return rooms;
        }finally {
            rd.close();
        }
    }

}
